'use strict';
app.controller('loginCtrl',['$scope','$state','$cookies','loginService', function($scope,$state,$cookies,loginService){

    $scope.dis = false;
    $scope.butName = '登录';
    // type: 0 is shop,method: 0 is account.
    $scope.auth = {
        type:0,
        method:0
    };

    $scope.resultStatus = {
        isError:false,
        errorMsg:''
    }

    $scope.login = function(){

        var pattern = /^1[3|4|5|7|8]\d{9}$/;
        if(!pattern.test($scope.auth.loginName)){
            $scope.resultStatus.isError = true;
            $scope.resultStatus.errorMsg = '请填写正确的手机号！';
            return;
        }else{
            $scope.resultStatus.isError = false;
            $scope.resultStatus.errorMsg = '';
        }

        $scope.dis = true;
        $scope.butName = '登录中...';
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(publicKey);
        $scope.auth.loginToken = encodeURIComponent(encrypt.encrypt($scope.auth.password));
        delete $scope.auth["password"];

        loginService.login($scope).then(function(result){
            $scope.dis = false;
            $scope.butName = '登录';

            switch (result.status){
                case 200:
                    var obj = result.data;

                    //delete obj["systemUserId"];
                    delete obj["logoutTime"];
                    for(var key in obj){
                        setCookie(key,[obj[key]]);
                    }

                    if(obj.isMainAccount==1){
                            //非连锁情况下
                        if(obj.isChain==0 && obj.isImproveShop==1&& obj.isImproveMeal==1){
                            //shop
                            location.href=$scope.app.shopHomeUrl;
                        }else if(obj.isChain==1 &&obj.isImproveChain==1 && obj.isImproveMeal==1){
                            //chain
                            location.href=$scope.app.chainHomeUrl;
                        }else{
                            $state.go('access.register.improve',{id:obj.systemUserId});
                        }
                    }else{
                        if(obj.isChain==0){
                            //shop
                            location.href=$scope.app.shopHomeUrl;
                        }else if(obj.isChain==1){
                            //chain
                            location.href=$scope.app.chainHomeUrl;
                        }
                    }

                    break;
                case 601:
                    $scope.resultStatus.errorMsg = '登录账号或者密码错误！';
                    break;
                case 608:
                    $scope.resultStatus.errorMsg = '登录证书错误，请刷新或重启浏览器再试！';
                    break;
                case 609:
                    $scope.resultStatus.errorMsg = '该账号已被冻结，请联系管理员！';
                    break;
                case 610:
                    $scope.resultStatus.errorMsg = '无法获取登录信息，请刷新或重启浏览器再试！';
                    break;
                default :
                    $scope.resultStatus.errorMsg = '发生未知错误！请联系管理员';
                    break
            }
        });
    }

    var setCookie=function(name, value) {
        //path=/表示全站有效，而不是当前页
        document.cookie = name + "=" + value + ";path="+$scope.app.cookiePath;
    }

}]);